package com.rnd.kx.mvp.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.rnd.kx.mvp.bean.IndexBean;
import com.squareup.picasso.Picasso;

import java.util.List;

public class BannerAdapter extends PagerAdapter {
    private List<IndexBean.AdvBean> advBeans;
    private Context context;
    private ImageLoader imageLoader;

    public BannerAdapter(List<IndexBean.AdvBean> advBeans, Context context, ImageLoader imageLoader) {
        this.advBeans = advBeans;
        this.context = context;
        this.imageLoader = imageLoader;
    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
        return view == o;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        int i = position % advBeans.size();
        ImageView imageView = new ImageView(context);
        Picasso.with(context)
                .load(advBeans.get(i).getCover())
                .into(imageView);
        //imageLoader.getInstance().displayImage(advBeans.get(i).getCover(),imageView);
        //imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        container.addView(imageView);
        return imageView;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView(((View) object));
    }

}
